﻿#region License
/* ---------------------------------------------------------------- *
 *
 * This file is part of the Xcoordination Application Space
 * ("XcoAppSpace") http://www.xcoordination.com
 *
 * Copyright (C) 2009 Xcoordination http://www.xcoordination.com
 *
 * XcoAppSpace is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU General
 * Public License as published by the Free Software Foundation.
 *
 * XcoAppSpace is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/
 * or write to the Free Software Foundation, Inc., 51 Franklin
 * Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * ---------------------------------------------------------------- */
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using XcoAppSpaces.Contracts.Service;
using XcoAppSpaces.Core.Ports;

namespace XcoAppSpaces.Contracts.Worker
{
	/// <summary>
	/// Delegate used for events when the worker is added or removed.
	/// </summary>
	/// <param name="sender">The IXcoWorkerRegistry instance that triggered the event.</param>
	/// <param name="workerInfo">Information about the worker that has been added or removed.</param>
	public delegate void WorkerEventHandler(object sender, XcoWorkerInfo workerInfo);

	/// <summary>
	/// Interface for class that provides access to the registry of local workers. Allows
	/// to resolve workers, check if a worker exists and register new ones.
	/// </summary>
	public interface IXcoWorkerRegistry : IXcoService
	{
		/// <summary>
		/// Event that is triggered when a new worker is added to the worker registry.
		/// </summary>
		event WorkerEventHandler WorkerAdded;

		/// <summary>
		/// Event that is triggered when an existing worker is removed form the worker registry.
		/// </summary>
		event WorkerEventHandler WorkerRemoved;

		/// <summary>
		/// Registers a new worker.
		/// </summary>
		/// <typeparam name="TContract">The contract type of the worker.</typeparam>
		/// <param name="instance">The worker instance to be registered.</param>
		/// <param name="name">The name of the worker (null if the worker doesn't have a name).</param>
		void Register<TContract>(TContract instance, string name) where TContract : IPort;
		
		/// <summary>
		/// Checks if a certain worker exists.
		/// </summary>
		/// <param name="workerIdentifier">Identifier of the worker, built out of the worker's type and name.</param>
		/// <param name="onlyPublished">Only search for published workers.</param>
		/// <param name="connectionRequest">True if the method is called because of a connection request.</param>
		/// <returns>True if a worker with the given type and name exists.</returns>
		bool Exists(string workerIdentifier, bool onlyPublished, bool connectionRequest);
		
		/// <summary>
		/// Resolves a worker from the registry and returns an info object that contains
		/// the worker object and further information about the worker.
		/// </summary>
		/// <param name="workerIdentifier">Identifier of the worker, built out of the worker's type and name.</param>
		/// <param name="onlyPublished">Only search for published workers.</param>
		/// <returns>A worker info object for the worker, if one with the given type and name exists, otherwise null.</returns>
		XcoWorkerInfo Resolve(string workerIdentifier, bool onlyPublished);

		/// <summary>
		/// Resolves a worker from the registry.
		/// </summary>
		/// <typeparam name="TMessageContract">The contract type of the worker.</typeparam>
		/// <param name="name">The name of the worker.</param>
		/// <returns>The worker instance, if one with the given type and name exists, otherwise null.</returns>
		TMessageContract Resolve<TMessageContract>(string name);

		/// <summary>
		/// Generates an identifier string for the given worker type and name.
		/// </summary>
		/// <param name="workerType">Worker type.</param>
		/// <param name="workerName">Worker name (or null, if the worker doesn't have a name).</param>
		/// <returns></returns>
		string GenerateIdentifier(Type workerType, string workerName);
	}
}
